import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      component: () => import('@/views/layout'),
      redirect: '/dashboard',
      children: [
        { path: 'dashboard', component: () => import('@/views/dashboard') },
        { path: 'article', component: () => import('@/views/article') }
      ]
    },
    { path: '/login', component: () => import('@/views/login') }
  ]
})

// 将登录页设成白名单
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token || whiteList.includes(to.path)) return next()
  // next(路径) 拦截到某个路径页面
  next('/login')
})
export default router
